package cn.tinlan.shopcoin.app.koi.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.TimeUtil;
import cn.tinlan.shopcoin.app.koi.KoiMessageActivity;
import cn.tinlan.shopcoin.app.koi.model.KoiListItemModel;
import cn.tinlan.shopcoin.app.koi.model.KoiSeekModel;

/**
 * Created by Mortimer on 2018/12/24. 17:24
 * 佛祖🙏，永无bug。
 */
public class KoiListAdapter extends BaseQuickAdapter<KoiListItemModel,BaseViewHolder> {

    private final int Head_TYPE = 1;
//    private int listStatus = 1;
    //用于退出activity,避免countdown，造成资源浪费。
    private SparseArray<CountDownTimer> countDownMap;
    private KoiSeekModel seekModel;
    private int layoutHeadId;

    public KoiListAdapter(int layoutResId,int layoutHeadId, @Nullable List data) {
        super(layoutResId, data);
//        this.listStatus = listStatus;
        this.layoutHeadId = layoutHeadId;
        countDownMap = new SparseArray<>();
    }


    @Override
    protected int getDefItemViewType(int position) {
//        Log.d(">>>>>>>>>>>","000000001111111111222222222222");
//        if (position == 0) {
//            Log.d(">>>>>>>>>>>","------------1111111111222222222222");
//            return Head_TYPE;
//        }
        return super.getDefItemViewType(position);
    }



    @Override
    protected void convert(BaseViewHolder helper, KoiListItemModel item) {


        if (helper instanceof KoiListViewHolder) {
            final  KoiListViewHolder helper1;
            helper1 = (KoiListViewHolder) helper;
            final String id = item.getId();

            TextView status = helper1.getView(R.id.koi_list_itv);
            TextView timeStatus = helper1.getView(R.id.koi_listitem_ts);
//            if (listStatus == 1) {
                if (item.getStatus().equals("1")){
                    status.setEnabled(true);
                    status.setText("立即赠送");
                    timeStatus.setText("待送出截止时间还有：");
                    status.setBackgroundColor(Color.parseColor("#FD7F35"));
                }else {
                    status.setEnabled(false);
                    status.setText("已送出");
                    timeStatus.setText("待确认截止时间还有：");
                    status.setBackgroundColor(Color.parseColor("#C0C0C0"));
                }
//            }

            status.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemSubViewClickListener != null) {
                        onItemSubViewClickListener.onItemStatusClick(id);
                    }
                }
            });


            helper1.setText(R.id.koi_listitem_code,"对照码："+item.getOrder_sn());
            helper1.setText(R.id.koi_listitem_leftcoin,"送出"+item.getAccount()+"锦");

            String mobile = item.getMobile();
            String maskNumber = mobile.substring(0,3)+"****"+mobile.substring(7,mobile.length());
            helper1.setText(R.id.koi_listitem_account,"接受账号："+maskNumber);
//            helper1.setText(R.id.koi_listitem_wx,"微信："+item.getWx());
//            helper1.setText(R.id.koi_listitem_zfb,"支付宝："+item.getZfb());
            helper1.setText(R.id.koi_listitem_rightcoin,item.getDcc_account());

            String dcc_amount = item.getDcc_amount();
            String mask_dcc_amount = dcc_amount.substring(0,3)+"****"+dcc_amount.substring(7,dcc_amount.length());

            helper1.setText(R.id.koi_listitem_rightaccount,"接受DCC账号："+mask_dcc_amount);
//        helper.setText(R.id.koi_listitem_ra,);


            long finishTime = Integer.parseInt(item.getPay_out_time());

            long time = finishTime*1000 - System.currentTimeMillis();
//             Log.d("currenttime",System.currentTimeMillis()+"");
//            Log.d("houtaicurrenttime",finishTime+"time"+time);

            if (helper1.countDownTimer != null) {
                helper1.countDownTimer.cancel();
            }

            if (time > 0) {
                helper1.countDownTimer = new CountDownTimer(time,1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        helper1.setText(R.id.koi_listitem_time, TimeUtil.getCountTimeByLong(millisUntilFinished));
                    }

                    @Override
                    public void onFinish() {
                        helper1.setText(R.id.koi_listitem_time,"结束");
                    }
                }.start();

            } else {
                helper1.setText(R.id.koi_listitem_time,"结束");
            }
        }else if (helper instanceof KoiHeadViewHolder){
//            Log.d(">>>>>>>>>>>","11111111112323232232");
            KoiHeadViewHolder headViewHolder = (KoiHeadViewHolder)helper;

        }

//        Log.d(">>>>>>>>>>>","1111111111");

//        ImageView leftIMG = helper.getView(R.id.koi_list_iimg);
//
//        leftIMG.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (onItemSubViewClickListener != null) {
//                    onItemSubViewClickListener.onItemIMGClick(id);
//                }
//            }
//        });



//        else {
//
//            if (item.getStatus().equals("1")){
//                status.setEnabled(true);
//                status.setText("未赠送");
//                timeStatus.setText("待接收时间还有：");
//                status.setBackgroundColor(Color.parseColor("#FD7F35"));
//            }else if (item.getStatus().equals("2")){
//                status.setEnabled(false);
//                status.setText("待确认");
//                timeStatus.setText("待确认时间还有：");
//                status.setBackgroundColor(Color.parseColor("#C0C0C0"));
//            }else if (item.getStatus().equals("3")){
//                status.setEnabled(false);
//                status.setText("已确认");
//                timeStatus.setText("后台审核中：");
//                status.setBackgroundColor(Color.parseColor("#C0C0C0"));
//            }else {
//                status.setEnabled(false);
//                status.setText("已确认");
//                timeStatus.setText("交易完成：");
//                status.setBackgroundColor(Color.parseColor("#C0C0C0"));
//            }
//
//        }






//        TextView message = helper.getView(R.id.koi_listitem_message);
//        message.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (onItemSubViewClickListener != null) {
//                    onItemSubViewClickListener.onItemMessageClick(id);
//                }
//            }
//        });
//
//        TextView messageList = helper.getView(R.id.koi_listitem_mlist);
//        messageList.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(mContext, KoiMessageActivity.class);
//
//                mContext.startActivity(intent);
//            }
//        });
    }

//    @Override
//    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
//
//    }

    public void upAdapter(List<KoiListItemModel> data, KoiSeekModel seekModel) {
        setNewData(data);
        this.seekModel = seekModel;
    }


    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {

        Log.d(">>>>>>>>>>>","1111111111222222222222");
//        if (viewType == Head_TYPE) {
//            Log.d(">>>>>>>>>>>","11111111112222222222223333333");
//            return new KoiHeadViewHolder(getItemView(layoutHeadId,parent));
//        }

        return new KoiListViewHolder(getItemView(R.layout.view_koi_listitem,parent));

//        return super.onCreateDefViewHolder(parent, viewType);
    }
    /**
     * 清空资源
     */
    public void cancelAllTimers() {
        if (countDownMap == null) {
            return;
        }
//        Log.e("TAG",  "size :  " + countDownMap.size());
        for (int i = 0,length = countDownMap.size(); i < length; i++) {
            CountDownTimer cdt = countDownMap.get(countDownMap.keyAt(i));
            if (cdt != null) {
                cdt.cancel();
            }
        }
    }

    private OnItemSubViewClickListener onItemSubViewClickListener;

   public interface OnItemSubViewClickListener {
//        public void onItemIMGClick(String id);
        public void onItemStatusClick(String id);
//        public void onItemMessageClick(String id);
   }

    public void setOnItemSubViewClickListener(OnItemSubViewClickListener onItemSubViewClickListener) {
        this.onItemSubViewClickListener = onItemSubViewClickListener;
    }

    public class KoiListViewHolder extends BaseViewHolder {
        public CountDownTimer countDownTimer;
        //        View foot;
        public KoiListViewHolder(View view) {
            super(view);
//            foot = view.findViewById(R.id.address_list_foot);
        }
    }

    public class KoiHeadViewHolder extends BaseViewHolder {

        public KoiHeadViewHolder(View view) {
            super(view);
        }
    }

}
